package ru.alt22.visor.php.ui.swt;

import java.util.ArrayList;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.php.internal.core.phpModel.phpElementData.PHPClassData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;

import ru.alt22.visor.php.ui.providers.PHPClassesConentProvider;
import ru.alt22.visor.php.ui.providers.PHPElementLabelProvider;

public class PHPElementSelector extends Composite {
	private TableViewer tableViewer;

	public PHPElementSelector(Composite parent, int style) {
		this(parent, style, new PHPClassesConentProvider());
	}

	public PHPElementSelector(Composite parent, int style, IStructuredContentProvider contentProvider) {
		super(parent, SWT.NONE);
		this.setLayout(new FillLayout());

		tableViewer = new TableViewer(this, style);
		tableViewer.setContentProvider(contentProvider);
		tableViewer.setLabelProvider(new PHPElementLabelProvider());

		getTable().setHeaderVisible(false);
		getTable().setLinesVisible(false);
	}

	public PHPClassData[] getSelection() {
		ArrayList<PHPClassData> result = new ArrayList<PHPClassData>();
		for (int i = 0; i < getTable().getSelectionCount(); i ++) {
			PHPClassData classData = (PHPClassData) getTable().getSelection()[i].getData();
			result.add(classData);
		}
		return result.toArray(new PHPClassData[result.size()]);
	}

	public Table getTable() {
		return tableViewer.getTable();
	}

	public void addRow(PHPClassData classData) {
		tableViewer.add(classData);
	}

	public PHPClassData getItem(int index) {
		return (PHPClassData) getTable().getItem(index).getData();
	}

	public PHPClassData[] getItems() {
		ArrayList<PHPClassData> result = new ArrayList<PHPClassData>();
		for (int i = 0; i < getItemCount(); i++) {
			result.add(getItem(i));
		}
		return result.toArray(new PHPClassData[getItemCount()]);
	}

	public int getSelectionIndex() {
		return getTable().getSelectionIndex();
	}

	public void select(int index) {
		getTable().select(index);
	}

	public void removeAll() {
		getTable().removeAll();
	}

	public int getItemCount() {
		return getTable().getItemCount();
	}

}